package states;

import hypeerweb.Node;
import hypeerweb.Range;

/**
 * The Standard node state, where we look for a smaller
 * neighbor or that has no children.
 *
 * @author brinkerhoff_kendra
 *
 */
public class StandardNodeState extends NodeState {

	/**
	 * Default constructor, sets the current state of the machine.
	 */
	public StandardNodeState() {
		STATE_ID = 0;
	}

	/**
	 * Replaces range.upperBound with its smallest neighbor without children.  
	 * 
	 * @pre range &ne; null AND
	 * 		<br>&exist; smaller node without childen &isin; upperBound.neighbors
	 * @post result = UpPointingNodeState where upperBound' = upperBound.smallestNeighborWithoutChildren and
	 * 		<br>upperBound'.id < upperBound.id and
	 * 		<br>OR
	 * 		<br>result = null, meaning that there is an error in the hypeerweb connections
	 */
	public final NodeState shrinkRange(final Range r) {
		range = r;

		Node upper = range.getUpperBound();
		Node newUpper = upper.getSmallestNeighborWithoutChildren();

		if (newUpper == null) {
			System.out.println("upper's webId is " + upper.getWebId().getValue() + " " + upper.getConnections());
			return null;
		}
		else {
			range.setUpperBound(newUpper);
			return new UpPointingNodeState();
		}
	}

	/**
	 * @return "Standard Node State"
	 */
	public final String toString() {
		return "Standard Node State";
	}
}
